<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
 * See the file COPYRIGHT.html for more details.
 */
 
/*********************************************************************************
 * Displays a number as a currency according to the currency format for the
 * specified locale
 * @param decimal $amount Amount that you want to format.
 * @param int $decimals number of decimals
 * @return string
 * @access public
 *********************************************************************************
 */
function moneyFormat($amount,$decimals){
  // get local info
  $localeInfo = localeconv();
  if (!$localeInfo["negative_sign"]) {
    $localeInfo["negative_sign"] = '-';
  }
  if ($amount < 0) {
    $prefix = "n";
    $sign = $localeInfo["negative_sign"];
  } else {
    $prefix = "p";
    $sign = $localeInfo["positive_sign"];
  }
  // Disabling following line forces currency symbol. Recommended for Admin, Library Settings, HTML Charset: utf-8
  $currencySymbol = $localeInfo["currency_symbol"];
  if (!H($currencySymbol)) {
    $currencySymbol = '$';
  }
  $dec_point = $localeInfo["mon_decimal_point"];
  if (!$dec_point) {
    $dec_point = '.';
  }
  $thousand_sep = $localeInfo["mon_thousands_sep"];
  if (!$thousand_sep) {
    $thousand_sep = ',';
  }
  if ($localeInfo[$prefix."_sep_by_space"]) {
    $sep = " ";
  } else {
    $sep = "";
  }

  // format number
  $result = number_format(abs($amount),$decimals,$dec_point,$thousand_sep);

/* add currency symbol and sign
   _sign_posn doc:
   0 Parentheses surround the quantity and currency_symbol  
   1 The sign string precedes the quantity and currency_symbol  
   2 The sign string succeeds the quantity and currency_symbol  
   3 The sign string immediately precedes the currency_symbol  
   4 The sign string immediately succeeds the currency_symbol  
*/

  if ($localeInfo[$prefix."_sign_posn"] == 0) {
    if ($localeInfo[$prefix."_cs_precedes"]) {
      $result = "(".$currencySymbol.$sep.$result.")";
    } else {
      $result = "(".$result.$sep.$currencySymbol.")";
    }
  }elseif ($localeInfo[$prefix."_sign_posn"] == 1) {
    if ($localeInfo[$prefix."_cs_precedes"]) {
      $result = $sign.$currencySymbol.$sep.$result;
    } else {
      $result = $sign.$result.$sep.$currencySymbol;
    }
  }elseif ($localeInfo[$prefix."_sign_posn"] == 2) {
    if ($localeInfo[$prefix."_cs_precedes"]) {
      $result = $currencySymbol.$sep.$result.$sign;
    } else {
      $result = $result.$sep.$currencySymbol.$sign;
    }
  }elseif ($localeInfo[$prefix."_sign_posn"] == 3) {
    if ($localeInfo[$prefix."_cs_precedes"]) {
      $result = $sign.$currencySymbol.$sep.$result;
    } else {
      $result = $result.$sep.$sign.$currencySymbol;
    }
  }elseif ($localeInfo[$prefix."_sign_posn"] == 4) {
    if ($localeInfo[$prefix."_cs_precedes"]) {
      $result = $currencySymbol.$sign.$sep.$result;
    } else {
      $result = $result.$sep.$currencySymbol.$sign;
    }
  } else {
    # American-style default
    $result = $currencySymbol.$sep.$sign.$result;
  }
  
  return $result;
}

function UfValidStrCuit($cuit,&$resultado)
{

	$coeficiente[0]=5;
	$coeficiente[1]=4;
	$coeficiente[2]=3;
	$coeficiente[3]=2;
	$coeficiente[4]=7;
	$coeficiente[5]=6;
	$coeficiente[6]=5;
	$coeficiente[7]=4;
	$coeficiente[8]=3;
	$coeficiente[9]=2;

	$resultado=1;

	for ($i=0; $i < strlen($cuit); $i= $i +1) {    //separo cualquier caracter que no tenga que ver con numeros
		if ((Ord(substr($cuit, $i, 1)) >= 48) && (Ord(substr($cuit, $i, 1)) <= 57))
		{
			$cuit_rearmado = $cuit_rearmado . substr($cuit, $i, 1);
		}
	}

	If (strlen($cuit_rearmado) <> 11) {  // si to estan todos los digitos
		$resultado=0;
	} Else {
		$sumador = 0;
		$verificador = substr($cuit_rearmado, 10, 1); //tomo el digito verificador

		For ($i=0; $i <=9; $i=$i+1) {
			$sumador = $sumador + (substr($cuit_rearmado, $i, 1)) * $coeficiente[$i];//separo cada digito y lo multiplico por el coeficiente
		}

		$resultado = $sumador % 11;
		$resultado = 11 - $resultado;  //saco el digito verificador
		$veri_nro = intval($verificador);

		If ($veri_nro <> $resultado) {
			$resultado=0;
		} else {
			$cuit_rearmado = substr($cuit_rearmado, 0, 2) . "-" . substr($cuit_rearmado, 2, 8) . "-" . substr($cuit_rearmado, 10, 1);
		}
	}
}
	function cbu_valido($cbu){	
		if ( strlen($cbu) <> 22 ) {
			return false;
		}else{
			$digitos_cbu = str_split($cbu);
			for ($i = 0; $i<=21; $i++){
				if ( !is_numeric($digitos_cbu[$i]) ) 
					return false;
			}
			if ($digitos_cbu[7] <> verificador($digitos_cbu, 0, 6)) {
				return false;
			}
			if ($digitos_cbu[21] <> verificador($digitos_cbu, 8, 20)) {
				return false;
			}
			return true;
		}
	}
	
	function verificador($numero, $pos_inicial, $pos_final){
		$ponderador = array(3,1,7,9);
		$suma = 0;
		$j = 0;
		for ($i = $pos_final; $i >= $pos_inicial; $i--){
			$suma = $suma + ($numero[$i] * $ponderador[$j % 4]);
			$j++;
		}
		$digito = (10 - $suma % 10) % 10;
		return $digito;
	}	
?>
